# res://scripts/Character.gd
# Represents a basic character in the battle system.

extends Node

class_name Character

# Character properties based on the GDD
enum Role { GUARDIAN, ASSAULT, SUPPORT } # 守護者, 強襲者, 支援者

@export var character_name: String = "Unnamed Character"
@export var role: Role = Role.ASSAULT
@export var max_hp: int = 100
@export var current_hp: int = 100
@export var max_sp: int = 100
@export var current_sp: int = 50
@export var speed: int = 10
@export var atk: int = 20
@export var def: int = 10
@export var mnd: int = 15 # Mind for healing/effects

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func take_damage(damage: int) -> bool:
	current_hp = max(0, current_hp - damage)
	print("%s takes %d damage. HP: %d/%d" % [character_name, damage, current_hp, max_hp])
	return current_hp <= 0 # Returns true if character is defeated

func restore_sp(amount: int):
	current_sp = min(max_sp, current_sp + amount)
	print("%s restores %d SP. SP: %d/%d" % [character_name, amount, current_sp, max_sp])

func consume_sp(amount: int) -> bool:
	if current_sp >= amount:
		current_sp -= amount
		print("%s consumes %d SP. SP: %d/%d" % [character_name, amount, current_sp, max_sp])
		return true
	else:
		print("%s cannot consume %d SP. Not enough SP. Current SP: %d" % [character_name, amount, current_sp])
		return false

# Simulate a character's turn start, recovering SP and processing status effects
func on_turn_start():
	restore_sp(15) # As per GDD
	# TODO: Process Buffs/Debuffs
	
# Simulate a character performing an action (attack/skill)
# This is a simplified version for prototyping
func perform_action(target: Character, action_type: String = "attack", sp_cost: int = 0) -> bool:
	if not consume_sp(sp_cost):
		return false
		
	if action_type == "attack":
		# Simplified damage calculation for prototype
		var damage = max(1, atk - target.def / 2) 
		print("%s performs standard attack on %s for %d damage." % [character_name, target.character_name, damage])
		return target.take_damage(damage)
	elif action_type == "skill":
		# Placeholder for a skill
		var damage = max(1, atk * 1.5 - target.def / 2)
		print("%s uses Skill on %s for %d damage." % [character_name, target.character_name, damage])
		return target.take_damage(damage)
	elif action_type == "ultimate":
		# Placeholder for an ultimate
		var damage = max(1, atk * 2.5 - target.def / 2)
		print("%s uses Ultimate on %s for %d damage." % [character_name, target.character_name, damage])
		return target.take_damage(damage)
	else:
		print("Unknown action type: %s" % action_type)
		return false
		
# Simulate receiving an attack and the player's reaction (guard)
# Returns damage taken after guard
func receive_attack(damage: int, is_perfect_guard: bool = false) -> int:
	if is_perfect_guard:
		restore_sp(15) # As per GDD for perfect guard
		print("%s performed a PERFECT GUARD! Immune to attack and restored SP." % character_name)
		return 0
	else:
		# Standard guard reduces damage by 70% according to GDD
		var guarded_damage = int(damage * 0.3) 
		print("%s performed a STANDARD GUARD! Damage reduced to %d." % [character_name, guarded_damage])
		take_damage(guarded_damage)
		return guarded_damage